package com.ch.clinking.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ch.clinking.R;
import com.ch.clinking.bean.StockChanged;

import java.util.List;

public class StockChangedAdapter extends RecyclerView.Adapter<StockChangedAdapter.StockChangedViewHolder> {
    private List<StockChanged> stockChangedList;
    private Context context;

    public StockChangedAdapter(Context context, List<StockChanged> stockChangedList) {
        this.context = context;
        this.stockChangedList = stockChangedList;
    }

    @NonNull
    @Override
    public StockChangedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_stock_changed, parent, false);
        return new StockChangedViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StockChangedViewHolder holder, int position) {
        StockChanged stockChanged = stockChangedList.get(position);

        // 设置昵称和时间
        holder.tvNickname.setText(stockChanged.getNickName());
        holder.createTime.setText(stockChanged.getCreatedTime().substring(5, stockChanged.getCreatedTime().length() - 3));

        // 清空表格
        holder.tlStockData.removeAllViews();

        // 动态生成表格行
        TableRow sizeRow = new TableRow(context);
        TableRow stockRow = new TableRow(context);
        TableRow finalStockRow = new TableRow(context);

        TableRow.LayoutParams params = new TableRow.LayoutParams(120, TableRow.LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;

        addTableCell(sizeRow, stockRow, finalStockRow, "尺码", "变更", "剩余", params);

        addSizeAndStockToTable(sizeRow, stockRow, finalStockRow, stockChanged, params);

        holder.tlStockData.addView(sizeRow);
        holder.tlStockData.addView(stockRow);
        holder.tlStockData.addView(finalStockRow);
    }

    private void addSizeAndStockToTable(TableRow sizeRow, TableRow stockRow, TableRow finalStockRow, StockChanged stockChanged, TableRow.LayoutParams params) {
        if (stockChanged.isActivate_XXS()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "XXS", String.valueOf(stockChanged.getChange_XXS()), String.valueOf(stockChanged.getInHouseStock_XXS()), params);
        }
        if (stockChanged.isActivate_XS()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "XS", String.valueOf(stockChanged.getChange_XS()), String.valueOf(stockChanged.getInHouseStock_XS()), params);
        }
        if (stockChanged.isActivate_S()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "S", String.valueOf(stockChanged.getChange_S()), String.valueOf(stockChanged.getInHouseStock_S()), params);
        }
        if (stockChanged.isActivate_M()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "M", String.valueOf(stockChanged.getChange_M()), String.valueOf(stockChanged.getInHouseStock_M()), params);
        }
        if (stockChanged.isActivate_L()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "L", String.valueOf(stockChanged.getChange_L()), String.valueOf(stockChanged.getInHouseStock_L()), params);
        }
        if (stockChanged.isActivate_XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "XL", String.valueOf(stockChanged.getChange_XL()), String.valueOf(stockChanged.getInHouseStock_XL()), params);
        }
        if (stockChanged.isActivate_XXL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "XXL", String.valueOf(stockChanged.getChange_XXL()), String.valueOf(stockChanged.getInHouseStock_XXL()), params);
        }
        if (stockChanged.isActivate_Plus_0XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "0XL", String.valueOf(stockChanged.getChange_Plus_0XL()), String.valueOf(stockChanged.getInHouseStock_Plus_0XL()), params);
        }
        if (stockChanged.isActivate_Plus_1XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "1XL", String.valueOf(stockChanged.getChange_Plus_1XL()), String.valueOf(stockChanged.getInHouseStock_Plus_1XL()), params);
        }
        if (stockChanged.isActivate_Plus_2XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "2XL", String.valueOf(stockChanged.getChange_Plus_2XL()), String.valueOf(stockChanged.getInHouseStock_Plus_2XL()), params);
        }
        if (stockChanged.isActivate_Plus_3XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "3XL", String.valueOf(stockChanged.getChange_Plus_3XL()), String.valueOf(stockChanged.getInHouseStock_Plus_3XL()), params);
        }
        if (stockChanged.isActivate_Plus_4XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "4XL", String.valueOf(stockChanged.getChange_Plus_4XL()), String.valueOf(stockChanged.getInHouseStock_Plus_4XL()), params);
        }
        if (stockChanged.isActivate_Plus_5XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "5XL", String.valueOf(stockChanged.getChange_Plus_5XL()), String.valueOf(stockChanged.getInHouseStock_Plus_5XL()), params);
        }
        if (stockChanged.isActivate_Plus_6XL()) {
            addTableCell(sizeRow, stockRow, finalStockRow, "6XL", String.valueOf(stockChanged.getChange_Plus_6XL()), String.valueOf(stockChanged.getInHouseStock_Plus_6XL()), params);
        }
    }

    private void addTableCell(TableRow sizeRow, TableRow stockRow, TableRow finalStockRow, String sizeText, String stockValue, String finalStock, TableRow.LayoutParams params) {
        TextView size = new TextView(context);
        size.setText(sizeText);
        size.setLayoutParams(params);
        size.setGravity(Gravity.CENTER);
        size.setWidth(120);
        size.setPadding(0,0,0,0);
        sizeRow.addView(size);

        TextView stock = new TextView(context);
        stock.setText(String.valueOf(stockValue));
        stock.setLayoutParams(params);
        stock.setGravity(Gravity.CENTER);
        stock.setWidth(120);
        stock.setPadding(0,0,0,0);
        stockRow.addView(stock);

        TextView finalStockView = new TextView(context);
        finalStockView.setText(String.valueOf(finalStock));
        finalStockView.setLayoutParams(params);
        finalStockView.setGravity(Gravity.CENTER);
        finalStockView.setWidth(120);
        finalStockView.setPadding(0,0,0,0);
        finalStockRow.addView(finalStockView);

    }

    @Override
    public int getItemCount() {
        return stockChangedList.size();
    }

    public static class StockChangedViewHolder extends RecyclerView.ViewHolder {
        TextView tvNickname,createTime;
        TableLayout tlStockData;

        public StockChangedViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNickname = itemView.findViewById(R.id.popup_stock_changed_nickname);
            createTime = itemView.findViewById(R.id.popup_stock_changed_time);
            tlStockData = itemView.findViewById(R.id.tl_stock_data);
        }
    }
}
